<?php
require_once('../piphp/mysql.php');

$action=$_POST['action'];


$data=json_decode($_POST['data']);

$tbname=$_POST['tbname'];

//初始化变量
$response="";
$roomsql=new mysql();

switch($action)
{
	case 'load':
		$sql=load($tbname);
		break;
	case 'maxID':
		$sql=maxID($tbname);
		break;

	default:
		$response="操作未成功";	
}

try{	
	if($action=='load')
	{
		//返回数据集
		$result=$roomsql->query($sql);
		//创建二维数组
		$rpsJSON=array();
		//提取指定表中的数据
		while($arr=$roomsql->fetch_array($result))
		{
			$temp=array(
				"id"	=>	$arr['id'],
				"start"	=>	$arr['startTime'],
				"end"	=>	$arr['endTime'],
				"title"	=>	$arr['title'],
				"body"	=>	$arr['content'],
				"duty"	=>	$arr['duty']
			);
			
			//向二维数组添加元素
			array_push($rpsJSON,$temp);
		}
		header('Content-Type:text/json');
		echo json_encode($rpsJSON);
		
	}
	else if($action=='maxID')
	{
		$result=$roomsql->query($sql);
		if($result==null)
			echo "0";
		else
		{
			$arr=$roomsql->fetch_array($result);
			echo $arr[0];
		}
	}
	else
	{
		$roomsql->query($sql);
		echo "操作成功，已经反映到数据库";
	}
	
}
catch(Exception $e)
{
	//数据库错误显示
	echo $response.'<br/>';
	$roomsql->errormsg($e);
}


function load($tbname)
{
	$sql="SELECT * FROM `room_$tbname`";
	return $sql;
}

?>